<template>
  <el-dialog
    :title="title"
    :visible.sync="drawer"
    width="50%"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="模板名称" prop="templateName" :rules="{ required: true, message: '模板名称不能为空'}">
        <div style="width: 60%;">
          <el-input v-model="form.templateName" placeholder="请输入模板名称" clearable size="small" />
        </div>
      </el-form-item>

      <el-form-item label="模板唯一值" prop="uniqueValue" :rules="{ required: true, message: '模板唯一值'}">
        <div style="width: 60%;">
          <el-input v-model="form.uniqueValue" :disabled="form.id ? true : false" placeholder="请输入模板唯一值" clearable size="small" />
        </div>
      </el-form-item>

      <el-form-item label="模板文件" prop="templateFile" :rules="{ required: true, message: '模板唯一值'}">
        <div style="width: 60%;">
          <FileUpload
            v-model="form.templateFile"
            :file-type="image.concat(video).concat(file)"
            :file-size="10"
            :limit="1"
          />

        </div>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{ form.id ? '立即修改' : '立即创建' }}</el-button>
        <el-button @click="handleClose">取消</el-button>
      </el-form-item>

    </el-form>
  </el-dialog>
</template>
<script>
// import { area } from '@/utils/area'
import request from '@/utils/request'

export default {

  props: {
    drawer: {
      type: Boolean,
      default: false
    },
    title: String,
    defaultValue: {
      type: Object,
      default: () => ({})
    },
    classifyList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      form: {

      },
      image: ['png', 'jpeg'],
      video: ['mp4'],
      file: ['pdf']
    }
  },
  watch: {
    defaultValue(val, val2) {
      this.form = {
        ...val
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const { templateFile } = this.form
          const { image, video, file } = this
          let type = '文件'

          const extension = this.getFileExtension(templateFile)
          if (image.includes(extension)) {
            type = '图片'
          } else if (video.includes(extension)) {
            type = '视频'
          }
          const p = {
            ...this.form,
            type
          }
          this.request(p)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getFileExtension(filename) {
      const lastDot = filename.lastIndexOf('.')
      return lastDot === -1 ? '' : filename.substring(lastDot + 1)
    },

    async request(params) {
      try {
        await request({
          url: 'system/template',
          method: params.id ? 'put' : 'post',
          data: params
        })
        this.$message({
          message: params.id ? '修改成功' : '添加成功',
          type: 'success',
          duration: 2000,
          onClose: () => {
            this.$emit('add-success')
            this.$refs['form'].resetFields()
          }
        })
      } catch (error) {
        this.$message({
          message: error,
          type: 'warning'
        })
      }
      //
    },
    handleClose() {
      this.$emit('close:drawer', false)
    }
  }
}
</script>

<style scoped>
  .item {
    display: flex;
    justify-content: right;
  }
  .required {
    color: red;
  }
</style>
